package com.icebartech.base.modules.user.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.icebartech.base.modules.user.dto.AdminUserCoreDTO;
import com.icebartech.base.modules.user.facade.AdminUserFacade;
import com.icebartech.base.modules.user.model.AdminUserCore;
import com.icebartech.base.modules.user.model.AppRole;
import com.icebartech.base.modules.user.service.AdminUserCoreService;
import com.icebartech.base.modules.user.service.AppRoleService;
import com.icebartech.core.enums.CommonResultCodeEnum;
import com.icebartech.core.vo.ResponseData;

@Service
public class AdminUserFacadeImpl implements AdminUserFacade {
	
	@Autowired
	private AdminUserCoreService adminUserCoreService;

	@Autowired
	private AppRoleService appUserRoleService;
	
	@Override
	public ResponseData<AdminUserCoreDTO> findUserCoreExtByUserId(Long userId) {
		AdminUserCoreDTO dto = adminUserCoreService.findUserCoreExtByUserId(userId);
		
		return ResponseData.builder(dto, CommonResultCodeEnum.SUCCESS);
	}

	@Override
	public ResponseData<String> getUserAvatarUrl(Long userId) {
		String avatarUrl = adminUserCoreService.getUserAvatarUrl(userId);
		return ResponseData.builder(avatarUrl, CommonResultCodeEnum.SUCCESS);
	}

	@Override
	public ResponseData<AdminUserCoreDTO> findAdminUserCoreByUserName(String userName) {
		AdminUserCore userCore = adminUserCoreService.findAdminUserCoreByUserName(userName);
		AdminUserCoreDTO dto = adminUserCoreService.assembleUserCoreExt(userCore);
		return ResponseData.builder(dto, CommonResultCodeEnum.SUCCESS);
	}

	@Override
	public ResponseData<List<AdminUserCoreDTO>> findAdminUserCoreListByRoleKey(String roleKey) {
		List<AdminUserCoreDTO> dataList = new ArrayList<>();
		AppRole appRole = appUserRoleService.findRoleByRoleKey(roleKey);
		if(appRole == null) {
			return ResponseData.builder(dataList, CommonResultCodeEnum.FAIL);
		}
		//限制最多一次拿100个
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("appRoleId", appRole.getId());
		PageInfo<AdminUserCoreDTO> pageInfo = adminUserCoreService.findAdminUserCoreByPage(paramMap, 1, 100);
		dataList = pageInfo.getList();
		return ResponseData.builder(dataList, CommonResultCodeEnum.SUCCESS);
	}

}
